﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;

namespace ClothingShopping.Util.SecurityHelper
{
    public static class EncryptionProcessing
    {
        #region md5+salt
        /// <summary>
        /// 获取MD5加密后字符串
        /// 加盐：将“-”换成“”
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string GetPwdMd5(string str)
        {
            MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            string result = BitConverter.ToString(md5.ComputeHash(bytes));

            return result.Replace("-", "");
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string MD5Encrypt32(string str)
        {
            str = str.Replace("1", "E");
            string encryptStr = "";
            MD5 mD5 = MD5.Create();
            byte[] md5Byte= mD5.ComputeHash(Encoding.UTF8.GetBytes(str));
            for(int i = 0; i < md5Byte.Length; i++)
            {
                encryptStr = encryptStr + md5Byte[i].ToString("X");
            }
            return encryptStr;
        }


        /// <summary>
        /// 根据salt对要加密的字符进行加密
        /// </summary>
        /// <param name="pwd">要加密的字符串</param>
        /// <param name="salt">加密salt</param>
        /// <returns></returns>
        public static string GetPwdhash(string pwd, string salt)
        {
            var db = new Rfc2898DeriveBytes(pwd, Convert.FromBase64String(salt));
            return Convert.ToBase64String(db.GetBytes(32));
        }
        #endregion
    }
}
